package com.zl.tby.controller;

import com.zl.tby.core.common.R;
import com.zl.tby.dto.TbyWildCameraAttachQuery;
import com.zl.tby.model.TbyAnimalPlant;
import com.zl.tby.model.TbyWildCameraAttach;
import com.zl.tby.service.ITbyAnimalPlantService;
import com.zl.tby.service.ITbyWildCameraAttachService;
import org.noear.solon.annotation.*;
import org.noear.solon.core.handle.MethodType;
import org.noear.solon.validation.annotation.NotNull;
import org.noear.solon.validation.annotation.Valid;

import java.util.List;

/**
 * 野保相机信息
 * @author changpeng
 * @version 1.0
 * @since 2024/8/6 20:22
 */
@Valid
@Controller
@Mapping("/wild_camera_attach")
public class TbyWildCameraAttachController {

    @Inject
    private ITbyWildCameraAttachService tbyWildCameraAttachService;
    @Inject
    private ITbyAnimalPlantService tbyAnimalPlantService;

    /**
     * 删除数据
     * @param id: 主键id
     * @return 结果
     */
    @Mapping(value = "/delete", method = MethodType.DELETE)
    public R<Void> delete(@NotNull Long id) {
        return this.tbyWildCameraAttachService.delete(id);
    }

    /**
     * 根据id查询
     * @param id: 主键id
     * @return 结果
     */
    @Mapping(value = "/get", method = MethodType.GET)
    public R<TbyWildCameraAttach> get(@NotNull Long id) {
        TbyWildCameraAttach entity = this.tbyWildCameraAttachService.get(id);
        return entity == null ? R.failed("数据不存在") : R.ok(entity);
    }

    /**
     * 分页查询
     * @param query: 查询参数
     * @return 分页结果
     */
    @Mapping(value = "/page", method = MethodType.GET)
    public R<?> page(@Body TbyWildCameraAttachQuery query) {
        return R.ok(this.tbyWildCameraAttachService.page(query));
    }

    /**
     * 列表查询
     * @param query: 查询参数
     * @return 列表结果
     */
    @Mapping(value = "/list", method = MethodType.GET)
    public R<List<TbyWildCameraAttach>> List(@Body TbyWildCameraAttachQuery query) {
        return R.ok(this.tbyWildCameraAttachService.list(query));
    }

    /**
     * 适配系统的物种修改
     * @param id
     * @param speciesId
     * @return
     */
    @Mapping(value = "/update_species_id", method = MethodType.GET)
    public R<Void> updateSpeciesId(@Param(value = "id") Long id, @Param("speciesId") Long speciesId) {
        TbyAnimalPlant ap = this.tbyAnimalPlantService.get(speciesId);
        if (ap == null) {
            return R.failed("物种不存在");
        }
        return this.tbyWildCameraAttachService.updateSpecies(id, ap.getId(), null, ap.getChName());
    }

    /**
     * 适配字典的物种修改
     * @param id
     * @param speciesCode
     * @param speciesName
     * @return
     */
    @Mapping(value = "/update_species_code", method = MethodType.POST)
    public R<Void> updateSpeciesCode(@Param(value = "id") Long id, @Param("speciesCode") String speciesCode, @Param("speciesName") String speciesName) {
        return this.tbyWildCameraAttachService.updateSpecies(id, null, speciesCode, speciesName);
    }
}
